package com.google.code.avajhelpdesk.pages.auth;

import org.apache.wicket.Application;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.value.ValueMap;
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.ValidationError;
import org.apache.wicket.validation.validator.StringValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.code.avajhelpdesk.entities.User;
import com.google.code.avajhelpdesk.pages.BasePage;
import com.google.code.avajhelpdesk.services.UserService;


public class SignUpPage extends BasePage {
	
	private static final Logger logger = LoggerFactory.getLogger(SignUpPage.class);
	
	private static class RegisterForm extends Form {

		private static final String PASSWORD_RETYPE_KEY = "password-retype";

		private static final String PASSWORD_KEY = "password";

		private static final String LAST_NAME_KEY = "last-name";

		private static final String FIRST_NAME_KEY = "first-name";

		private static final String LOGIN_KEY = "login";

		@SpringBean
		private UserService userService;
		
		private ValueMap model = new ValueMap();
		
		public RegisterForm(String id) {
			super(id);
			
			add(new TextField<String>(LOGIN_KEY, new PropertyModel<String>(model, LOGIN_KEY))
					.setRequired(true)
					.add(StringValidator.lengthBetween(User.MIN_USERNAME_LENGTH, User.MAX_USERNAME_LENGTH))
					.add(new IValidator<String>() {
						private static final long serialVersionUID = 1L;

						public void validate(IValidatable<String> validatable) {
							boolean userExists = userService.isUserAlreadyExists(validatable.getValue());
							if (userExists) {
								validatable.error(new ValidationError().setMessage("Login already in use"));
							}
						}
						
					}));
			
			add(new TextField<String>(FIRST_NAME_KEY, new PropertyModel<String>(model, FIRST_NAME_KEY))
					.setRequired(true));
			
			add(new TextField<String>(LAST_NAME_KEY, new PropertyModel<String>(model, LAST_NAME_KEY))
					.setRequired(true));
			
			add(new PasswordTextField(PASSWORD_KEY, new PropertyModel<String>(model, PASSWORD_KEY))
					.setRequired(true)
					.add(StringValidator.lengthBetween(User.MIN_PASSWORD_LENGTH, User.MAX_PASSWORD_LENGTH)));
			
			add(new PasswordTextField(PASSWORD_RETYPE_KEY, new PropertyModel<String>(model, PASSWORD_RETYPE_KEY))
					.setRequired(true));
			
			InjectorHolder.getInjector().inject(this);
		}
		
		@Override
		protected void onSubmit() {
			String login = model.getString(LOGIN_KEY); 
			String firstName = model.getString(FIRST_NAME_KEY);
			String lastName = model.getString(LAST_NAME_KEY);
			String password = model.getString(PASSWORD_KEY);
			String passwordRetype = model.getString(PASSWORD_RETYPE_KEY);
			
			if (!password.equals(passwordRetype)) {
				error("Mismatch password and retype");
				return;
			}
			
			userService.createUser(login, password, firstName, lastName);
			
			setResponsePage(Application.get().getHomePage());
		}
		
	}
	
	public SignUpPage() {
		add(new FeedbackPanel("register-form-feedback"));
		
		add(new RegisterForm("register-form"));
	}

	@Override
	protected String getPageTitle() {
		return "Sign up now!";
	}
}
